package co.edu.udem.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import co.edu.udem.domain.Place;
import co.edu.udem.domain.SubjectDetail;
import co.edu.udem.service.place.PlaceService;
import co.edu.udem.service.subject.detail.SubjectDetailService;

/**
 * handles the login process.
 * 
 * @author sgranada
 * 
 */
@Controller
public class SubjectDetailController extends BaseController {

	@Autowired
	private PlaceService placeService;

	@Autowired
	private SubjectDetailService subjectDetailService;

	@RequestMapping(value = "/subjectdetail/manage", method = RequestMethod.GET)
	public String getSubjecDetailtList(Model model,
			@RequestParam("id") String subjectCode) {

		logger.debug("Request recibido para mostrar los detalles de la materia");
		List<SubjectDetail> subjectDetailList = subjectDetailService
				.getSubjectDetailListBySubject(subjectCode);
		model.addAttribute("subjectsdetails", subjectDetailList);
		model.addAttribute("currentSubjectCode", subjectCode);
		return "subjectdetailpage";
	}

	@RequestMapping(value = "/subjectdetail/add", method = RequestMethod.GET)
	public String showAddSubjectDetailPage(Model model,
			@RequestParam("id") String subjectCode) {
		logger.debug("Request recibido para agregar los detalles de la materia");
		List<Place> placeList = placeService.getPlaceListByCriteria(null);
		model.addAttribute("placeCodeList", placeList);
		SubjectDetail subjectDetail = new SubjectDetail();
		subjectDetail.setSubjectCode(subjectCode);
		model.addAttribute("subjectDetailAttribute", subjectDetail);

		return "addsubjectdetailpage";
	}

	@RequestMapping(value = "/subjectdetail/add", method = RequestMethod.POST)
	public String addSubjectDetail(
			Model model,
			@ModelAttribute("subjectDetailAttribute") SubjectDetail subjectDetail) {
		logger.debug("Request recibido para agregar los detalles de la materia");
		subjectDetail.setSubjectDate(subjectDetail.getSubjectDateString());
		subjectDetailService.addSubjectDetail(subjectDetail);
		return "addedsubjectdetailpage";
	}
	
	@RequestMapping(value = "/subjectdetail/edit", method = RequestMethod.GET)
	public String edit(@RequestParam("id") String subjectDetailCode, Model model) {
		SubjectDetail subjectDetail = subjectDetailService.getSubjectDetailById(subjectDetailCode);
		subjectDetail.setSubjectDateString(subjectDetail.getSubjectDate());
		model.addAttribute("subjectDetailAttribute", subjectDetail);
		List<Place> placeList = placeService.getPlaceListByCriteria(null);
		model.addAttribute("placeCodeList", placeList);
		return "addsubjectdetailpage";
	}
	
	@RequestMapping(value = "/subjectdetail/delete", method = RequestMethod.GET)
	public String delete(@RequestParam("id") String subjectDetailCode) {
		subjectDetailService.delete(subjectDetailCode);
		return "deletedsubjectdetailpage";
	}
}
